﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;

namespace ClimbEdgesConvertor
{
    class Program
    {
        class Vertex
        {
            public float X, Y, Z;
        }

        class Poly
        {
            public Vertex A;
            public Vertex B;
            public Vertex C;
        }

        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Use .OBJ filename as parameter to convert it.");
                Console.WriteLine("ClimbEdgesConvertor <objfile> <outfile>");
            }
            else
            {
                List<Vertex> vertices = new List<Vertex>();
                List<Poly> edges = new List<Poly>();
                using (StreamReader reader = File.OpenText(args[0]))
                {
                    while (!reader.EndOfStream)
                    {
                        string line = reader.ReadLine();                        
                        if (line[0] == 'v')
                        {                            
                            Regex regex = new Regex(@"([\+-]?(\d+\.?(\d+)?|\.\d+))");
                            MatchCollection matches = regex.Matches(line);
                            if (matches.Count == 3)
                            {
                                Vertex newVertex = new Vertex();
                                newVertex.X = float.Parse(matches[0].Groups[0].Value, System.Globalization.CultureInfo.InvariantCulture);
                                newVertex.Y = float.Parse(matches[1].Groups[0].Value, System.Globalization.CultureInfo.InvariantCulture);
                                newVertex.Z = float.Parse(matches[2].Groups[0].Value, System.Globalization.CultureInfo.InvariantCulture);
                                vertices.Add(newVertex);
                            }
                        }
                        else if (line[0] == 'f')
                        {
                            Regex regex = new Regex(@"\d+");
                            MatchCollection matches = regex.Matches(line);
                            if (matches.Count == 3)
                            {
                                Poly newEdge = new Poly();
                                int ia = int.Parse(matches[0].Groups[0].Value) - 1 ;
                                int ib = int.Parse(matches[1].Groups[0].Value) - 1;
                                int ic = int.Parse(matches[2].Groups[0].Value) - 1;
                                if (ia < vertices.Count && ib < vertices.Count)
                                {
                                    newEdge.A = vertices[ia];
                                    newEdge.B = vertices[ib];
                                    newEdge.C = vertices[ic];
                                    edges.Add(newEdge);
                                }
                            }
                        }                                                   
                    }
                }                
                using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(args[1])))
                {
                    writer.Write(edges.Count);
                    Console.WriteLine("Vertex count: " + vertices.Count.ToString());
                    Console.WriteLine("Climb edges count: " + edges.Count.ToString());
                    foreach (Poly e in edges)
                    {
                        writer.Write(e.A.X);
                        writer.Write(e.A.Y);
                        writer.Write(e.A.Z);
                        writer.Write(e.B.X);
                        writer.Write(e.B.Y);
                        writer.Write(e.B.Z);
                        writer.Write(e.C.X);
                        writer.Write(e.C.Y);
                        writer.Write(e.C.Z);
                    }
                }
                Console.ReadKey();
            }           
        }
    }
}
